<template>
	<view style="padding: 20px;">
		<button @click="createLink">生成分享链接</button>
		<view v-if="link" style="margin-top: 20px;">
			<text style="font-size: 14px;">点击复制：</text>
			<text selectable style="font-size: 14px; color: #007AFF;">{{ link }}</text>
			<view @click="onclick">跳转</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'

	const link = ref('')
	
	function onclick(){
		uni.navigateTo({
			url:link.value
		})
	}

	const createLink = async () => {
		const pages = getCurrentPages()
		const route = pages[pages.length - 1].route // 如 'pages/test/test'
		console.log("route:", route)
		//const path = `/${route}?id=888&from=share`
		const path = `/pages/index/index`
		console.log("path:", path);
		const videoId = 10;
		const userId = 183;

		try {
			const [err, res] = await uni.request({
				url: 'http://localhost:443/api/wx/generate-url-link',
				method: 'POST',
				data: {
					path: 'pages/play/play',
					query: 'id=888', // ✅ 用 query，不是 scene
					env_version: 'trial',
					expire_type: 1,
					expire_interval: 7
				},
				success(res) {
					console.log('生成的链接:', res.data)
					link.value = res.data.url_link
					// https://wxaurl.cn/Njj1XOIzHfk
				}
			})


		} catch (e) {
			uni.showToast({
				title: '请求失败',
				icon: 'none'
			})
		}
	}
</script>